<?php  
	namespace Home\Controller;
	use Think\Controller;
    //权限节点管理
    //@author  程传民
	class NodesController extends AllowController{
		
		public function index(){
			$nodes = D("nodes");

			if(!empty($_GET['controllerName'])){
				$tot = $nodes->where("controllerName like '%".$_GET['controllerName']."%'")->count();
			}else{
				$tot = $nodes->count();
			}

			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	    	$search="";
	    	if(!empty($_GET['controllerName'])){
	    		$list = $nodes->where("controllerName like '%".$_GET['controllerName']."%'")->limit($page->firstRow,$page->listRows)->order("controllerName desc")->select();
	    		$search="/controllerName/{$_GET['controllerName']}";
	    	}else{
				$list = $nodes->limit($page->firstRow,$page->listRows)->order("controllerName desc")->select();
	    	}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("search",$search);
			$this->assign("list",$list);
			$this->display("Nodes/nodes");
		}

		//加载添加节点模板
		public function add(){
			$this->display("Nodes/add");
		}

		//添加节点
		public function doadd(){
			$date['remark'] = I("post.remark");
			//把控制器的第一个字母大写

			$date['controllerName'] = ucwords(I("post.controllerName"));
			$date['funName'] = I("post.funName");

			$nodes = D("nodes");

			$info = $nodes->add($date);

			if($info){
				echo "成功";
			}else{
				echo "失败";
			}

		}

		//加载修改节点模板
		public function edit(){
			$arr = array("id"=>I("get.id"));
			$nodes = D("nodes");
			$list = $nodes->where($arr)->find();
			$this->assign("list",$list);
			$this->display("Nodes/update");
		}

		//修改节点信息
		public function update(){
			$arr1 = array("id"=>I("post.id"));
			$data['remark'] = I("post.remark");
			$data['controllerName'] = I("post.controllerName");
			$data['funName'] = I("post.funName");
			$nodes = D("nodes");
			$info = $nodes->where($arr1)->save($data);
			if($info){
				echo "修改成功";
			}else{
				echo "修改失败";
			}
		}

		//删除节点
		public function delete(){
			$arr = array("id"=>I("post.id"));
			$nodes = D("nodes");
			$info = $nodes->where($arr)->delete();
			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

	}
?>